﻿using project.B12.IRepository;
using project.B12.IRepository.Base;
using project.B12.Model.Enums;
using project.B12.Model.Models;
using project.B12.Repository.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace project.B12.Repository.Repository
{
    public class BacteriaRepository : SugarRepository<Table_bacteria>, IBacteriaRepository
    {

        private IUnitOfWork UOW;
        public BacteriaRepository(IUnitOfWork pUOW) : base(pUOW)
        {
            UOW = pUOW;
        }

        /// <summary>
        /// 查询所有细菌
        /// </summary>
        /// <returns></returns>
        public List<Table_bacteria> QueryAllBacteria()
        {
            List<Table_bacteria> result;
            try
            {
                UOW.BeginTran();
                result = Query(o=>o.IsDelete==false).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 根据菌中文名称查询结果
        /// </summary>
        /// <returns></returns>
        public List<Table_bacteria> QueryByCNName(string pname)
        {
            List<Table_bacteria> result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.CN_Name == pname && x.IsDelete == false).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        public bool UpdateBacteria(Table_bacteria bacteria)
        {
            try
            {
                UOW.BeginTran();
                Update(bacteria);
                UOW.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
                return false;
            }
        }

        public void AddBacterias(Table_bacteria bacteria)
        {
            if (bacteria.Species != string.Empty)
            {
                bacteria.Level = BacteriaLevel.种;
            }
            else if (bacteria.Genus != string.Empty)
            {
                bacteria.Level = BacteriaLevel.属;
            }
            else if (bacteria.Family != string.Empty)
            {
                bacteria.Level = BacteriaLevel.科;
            }
            else if (bacteria.Order != string.Empty)
            {
                bacteria.Level = BacteriaLevel.目;
            }
            else if (bacteria.Class != string.Empty)
            {
                bacteria.Level = BacteriaLevel.纲;
            }
            else if (bacteria.Phylum != string.Empty)
            {
                bacteria.Level = BacteriaLevel.门;
            }
            else if (bacteria.Kingdom != string.Empty)
            {
                bacteria.Level = BacteriaLevel.界;
            }

            try
            {
                UOW.BeginTran();
                Add(bacteria);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }

        /// <summary>
        /// 根据菌Id和层级Id得到对应层级的name
        /// </summary>
        /// <param name="id"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public string QueryNamesByID(int id, BacteriaLevel level)
        {
            string name = null;
            try
            {
                UOW.BeginTran();
                List<Table_bacteria> bacterias = Query(x => x.Id==id&&x.IsDelete==false).Result;
                if (bacterias != null && bacterias.Count() != 0)
                {
                    Table_bacteria result = bacterias.First();
                    switch (level)
                    {
                        case BacteriaLevel.界:
                            name= result.Kingdom;
                            break;
                        case BacteriaLevel.门:
                            name = result.Phylum;
                            break;
                        case BacteriaLevel.纲:
                            name = result.Class;
                            break;
                        case BacteriaLevel.目:
                            name = result.Order;
                            break;
                        case BacteriaLevel.科:
                            name = result.Family;
                            break;
                        case BacteriaLevel.属:
                            name = result.Genus;
                            break;
                        case BacteriaLevel.种:
                            name = result.Species;
                            break;
                    }
                }
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return name;
        }

        public List<string> QueryByLevel(BacteriaLevel level)
        {
            List<string> levelNames = new List<string>();
            try
            {
                UOW.BeginTran();
                List<Table_bacteria>  result = Query(o=>o.IsDelete == false).Result;
                switch (level)
                {
                    case BacteriaLevel.界:
                        levelNames= result.Where(o=> o.Level == level).Select(o => o.Kingdom).Distinct().ToList();
                        break;
                    case BacteriaLevel.门:
                        levelNames = result.Where(o => o.Level == level).Select(o => o.Phylum).Distinct().ToList();
                        break;
                    case BacteriaLevel.纲:
                        levelNames = result.Where(o => o.Level == level).Select(o => o.Class).Distinct().ToList();
                        break;
                    case BacteriaLevel.目:
                        levelNames = result.Where(o => o.Level == level).Select(o => o.Order).Distinct().ToList();
                        break;
                    case BacteriaLevel.科:
                        levelNames = result.Where(o => o.Level == level).Select(o => o.Family).Distinct().ToList();
                        break;
                    case BacteriaLevel.属:
                        levelNames = result.Where(o => o.Level == level).Select(o => o.Genus).Distinct().ToList();
                        break;
                    case BacteriaLevel.种:
                        levelNames = result.Where(o => o.Level == level).Select(o => o.Species).Distinct().ToList();
                        break;
                }
                levelNames.RemoveAll(o => o == string.Empty);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                return levelNames;
            }
            return levelNames;
        }

        public int QueryIdByLevelName(BacteriaLevel level, string levelName)
        {
            int id = 0;
            try
            {
                UOW.BeginTran();
                List<Table_bacteria> result = Query(o => o.IsDelete == false).Result;
                switch (level)
                {
                    case BacteriaLevel.界:
                        id= result.Where(o => o.Kingdom == levelName&&o.Level==level).First().Id;
                        break;
                    case BacteriaLevel.门:
                        id = result.Where(o => o.Phylum== levelName && o.Level == level).First().Id;
                        break;
                    case BacteriaLevel.纲:
                        id = result.Where(o => o.Class==levelName && o.Level == level).First().Id;
                        break;
                    case BacteriaLevel.目:
                        id = result.Where(o => o.Order == levelName && o.Level == level).First().Id;
                        break;
                    case BacteriaLevel.科:
                        id = result.Where(o => o.Family == levelName && o.Level == level).First().Id;
                        break;
                    case BacteriaLevel.属:
                        id = result.Where(o => o.Genus == levelName && o.Level == level).First().Id;
                        break;
                    case BacteriaLevel.种:
                        id = result.Where(o => o.Species == levelName && o.Level == level).First().Id;
                        break;
                }
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                return 0;
            }
            return id;
        }

        public Table_bacteria QueryById(int id)
        {
            Table_bacteria result;
            try
            {
                UOW.BeginTran();
                result = Query(x => x.Id==id && x.IsDelete == false).Result.First();
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }
    }
}
